package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZsyyParkingCarLogAddBo;
import com.ruoyi.system.bo.ZsyyParkingCarLogQueryBo;
import com.ruoyi.system.bo.ZsyyParkingCarLogEditBo;
import com.ruoyi.system.domain.ZsyyParkingCarLog;
import com.ruoyi.system.mapper.ZsyyParkingCarLogMapper;
import com.ruoyi.system.vo.ZsyyParkingCarLogVo;
import com.ruoyi.system.service.IZsyyParkingCarLogService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * 车辆日志记录Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyParkingCarLogServiceImpl extends ServicePlusImpl<ZsyyParkingCarLogMapper, ZsyyParkingCarLog> implements IZsyyParkingCarLogService {
    @Autowired
    private ZsyyParkingCarLogMapper iZsyyParkingCarLogMapper;
    @Autowired
    private GetCommunityIdImpl getCommunityIdImpl;

    @Override
    public ZsyyParkingCarLogVo queryById(Long id){
        return getVoById(id, ZsyyParkingCarLogVo.class);
    }

    @Override
    public TableDataInfo<ZsyyParkingCarLogVo> queryPageList(ZsyyParkingCarLogQueryBo bo) {
    	bo.setCommunityId(getCommunityIdImpl.getLoginUser().getCommunityId());
        PagePlus<ZsyyParkingCarLog, ZsyyParkingCarLogVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), ZsyyParkingCarLogVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<ZsyyParkingCarLogVo> queryList(ZsyyParkingCarLogQueryBo bo) {
        return listVo(buildQueryWrapper(bo), ZsyyParkingCarLogVo.class);
    }

    private LambdaQueryWrapper<ZsyyParkingCarLog> buildQueryWrapper(ZsyyParkingCarLogQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsyyParkingCarLog> lqw = Wrappers.lambdaQuery();
		bo.setCommunityId(getCommunityIdImpl.getLoginUser().getCommunityId());
        lqw.like(StrUtil.isNotBlank(bo.getUsername()), ZsyyParkingCarLog::getUsername, bo.getUsername());
        lqw.like(StrUtil.isNotBlank(bo.getParkName()), ZsyyParkingCarLog::getParkName, bo.getParkName());
        lqw.eq(StrUtil.isNotBlank(bo.getCarLicenseNumber()), ZsyyParkingCarLog::getCarLicenseNumber, bo.getCarLicenseNumber());
        lqw.like(StrUtil.isNotBlank(bo.getCarName()), ZsyyParkingCarLog::getCarName, bo.getCarName());
        lqw.eq(bo.getCarType() != null, ZsyyParkingCarLog::getCarType, bo.getCarType());
        lqw.eq(StrUtil.isNotBlank(bo.getPhone()), ZsyyParkingCarLog::getPhone, bo.getPhone());
        lqw.between(StrUtil.isNotBlank(bo.getCreatetime()),ZsyyParkingCarLog::getCreatetime,bo.getCreatetime(),bo.getEndCreatetime());
		lqw.orderByDesc(ZsyyParkingCarLog::getCreatetime);
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyParkingCarLogAddBo bo) {
        ZsyyParkingCarLog add = BeanUtil.toBean(bo, ZsyyParkingCarLog.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ZsyyParkingCarLogEditBo bo) {
        ZsyyParkingCarLog update = BeanUtil.toBean(bo, ZsyyParkingCarLog.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyParkingCarLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
